package com.ruoyi.mes.systembasic.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SmesMaterialCategory;
import com.ruoyi.common.core.domain.entity.SmesProductCategory;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.systembasic.mapper.SmesMaterialCategoryMapper;
import com.ruoyi.mes.systembasic.service.ISmesMaterialCategoryService;

/**
 * 物料类别Service业务层处理
 *
 * @author simpsense
 * @date 2020-11-27
 */
@Service
public class SmesMaterialCategoryServiceImpl implements ISmesMaterialCategoryService
{
    @Autowired
    private SmesMaterialCategoryMapper smesMaterialCategoryMapper;

    /**
     * 查询物料类别
     *
     * @param materialCategoryId 物料类别ID
     * @return 物料类别
     */
    @Override
    public SmesMaterialCategory selectSmesMaterialCategoryById(Long materialCategoryId)
    {
        return smesMaterialCategoryMapper.selectSmesMaterialCategoryById(materialCategoryId);
    }

    /**
     * 查询物料类别列表
     *
     * @param smesMaterialCategory 物料类别
     * @return 物料类别
     */
    @Override
    public List<SmesMaterialCategory> selectSmesMaterialCategoryList(SmesMaterialCategory smesMaterialCategory)
    {
        return smesMaterialCategoryMapper.selectSmesMaterialCategoryList(smesMaterialCategory);
    }

    /**
     * 新增物料类别
     *
     * @param smesMaterialCategory 物料类别
     * @return 结果
     */
    @Override
    public int insertSmesMaterialCategory(SmesMaterialCategory smesMaterialCategory)
    {
        SmesMaterialCategory info = smesMaterialCategoryMapper.selectSmesMaterialCategoryById(smesMaterialCategory.getParentId());
//        // 如果父节点不为正常状态,则不允许新增子节点
//        if (!UserConstants.DEPT_NORMAL.equals(info.getDelFlag()))
//        {
//            throw new CustomException("部门停用，不允许新增");
//        }
        if(info!=null){
            smesMaterialCategory.setAncestors(info.getAncestors() + "," + smesMaterialCategory.getParentId());
        }
        smesMaterialCategory.setCreateTime(DateUtils.getNowDate());
        return smesMaterialCategoryMapper.insertSmesMaterialCategory(smesMaterialCategory);
    }

    /**
     * 修改物料类别
     *
     * @param smesMaterialCategory 物料类别
     * @return 结果
     */
    @Override
    public int updateSmesMaterialCategory(SmesMaterialCategory smesMaterialCategory)
    {
        SmesMaterialCategory newParentDept = smesMaterialCategoryMapper.selectSmesMaterialCategoryById(smesMaterialCategory.getParentId());
        SmesMaterialCategory oldDept = smesMaterialCategoryMapper.selectSmesMaterialCategoryById(smesMaterialCategory.getMaterialCategoryId());
        if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
        {
            String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getMaterialCategoryId();
            String oldAncestors = oldDept.getAncestors();
            smesMaterialCategory.setAncestors(newAncestors);
            updateCategoryChildren(smesMaterialCategory.getMaterialCategoryId(), newAncestors, oldAncestors);
        }
        smesMaterialCategory.setUpdateTime(DateUtils.getNowDate());
        return smesMaterialCategoryMapper.updateSmesMaterialCategory(smesMaterialCategory);
    }

    private void updateCategoryChildren(Long id, String newAncestors, String oldAncestors) {
        List<SmesMaterialCategory> children = smesMaterialCategoryMapper.selectChildrenCategoryById(id);
        for (SmesMaterialCategory child : children)
        {
            child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            smesMaterialCategoryMapper.updateCategoryChildren(children);
        }
    }

    /**
     * 批量删除物料类别
     *
     * @param materialCategoryIds 需要删除的物料类别ID
     * @return 结果
     */
    @Override
    public int deleteSmesMaterialCategoryByIds(Long[] materialCategoryIds)
    {
        return smesMaterialCategoryMapper.deleteSmesMaterialCategoryByIds(materialCategoryIds);
    }

    /**
     * 删除物料类别信息
     *
     * @param materialCategoryId 物料类别ID
     * @return 结果
     */
    @Override
    public int deleteSmesMaterialCategoryById(Long materialCategoryId)
    {
        return smesMaterialCategoryMapper.deleteSmesMaterialCategoryById(materialCategoryId);
    }

    @Override
    public List<TreeSelect> buildMaterialCategoryTreeSelect(List<SmesMaterialCategory> list) {
        List<SmesMaterialCategory> categoryTrees = buildCategoryTree(list);
        return categoryTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<SmesMaterialCategory> buildCategoryTree(List<SmesMaterialCategory> categories) {
        List<SmesMaterialCategory> returnList = new ArrayList<SmesMaterialCategory>();
        List<Long> tempList = new ArrayList<Long>();
        for (SmesMaterialCategory category : categories)
        {
            tempList.add(category.getMaterialCategoryId());
        }
        for (Iterator<SmesMaterialCategory> iterator = categories.iterator(); iterator.hasNext();)
        {
            SmesMaterialCategory category = (SmesMaterialCategory) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(category.getParentId()))
            {
                recursionFn(categories, category);
                returnList.add(category);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = categories;
        }
        return returnList;
    }
    /**
     * 递归列表
     */
    private void recursionFn(List<SmesMaterialCategory> list, SmesMaterialCategory t)
    {
        // 得到子节点列表
        List<SmesMaterialCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SmesMaterialCategory tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<SmesMaterialCategory> getChildList(List<SmesMaterialCategory> list, SmesMaterialCategory t)
    {
        List<SmesMaterialCategory> tlist = new ArrayList<SmesMaterialCategory>();
        Iterator<SmesMaterialCategory> it = list.iterator();
        while (it.hasNext())
        {
            SmesMaterialCategory n = (SmesMaterialCategory)it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getMaterialCategoryId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SmesMaterialCategory> list, SmesMaterialCategory t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}